after_initialize do
  Rails.logger.info("✅ [PLUGIN DEBUG] discourse-extended-product-fields plugin loaded")

  require_dependency 'post_creator'

  class ::PostCreator
    alias_method :original_create, :create

    def create
      Rails.logger.info("🔥 [PLUGIN DEBUG] PostCreator received opts: #{@opts.inspect}")

      post = original_create

      if post && @opts
        %i[product_category product_name product_price product_img_url].each do |field|
          value = @opts[field]
          if value.present?
            Rails.logger.info("✅ [PLUGIN DEBUG] Saving custom field #{field}: #{value}")
            post.custom_fields[field.to_s] = value
          end
        end

        post.save_custom_fields
      end

      post
    end
  end
end